{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "b7962124-2f45-49ac-a379-194657205ba9",
   "metadata": {},
   "source": [
    "- 接口文档：https://open.bigmodel.cn/dev/api#overview\n",
    "- APIkey地址：https://open.bigmodel.cn/usercenter/apikeys"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "e7381e2f-5bad-4f45-9d06-255cbdb231db",
   "metadata": {},
   "outputs": [],
   "source": [
    "import time\n",
    "import jwt\n",
    " \n",
    "def generate_token(apikey: str, exp_seconds: int):\n",
    "    try:\n",
    "        id, secret = apikey.split(\".\")\n",
    "    except Exception as e:\n",
    "        raise Exception(\"invalid apikey\", e)\n",
    " \n",
    "    payload = {\n",
    "        \"api_key\": id,\n",
    "        \"exp\": int(round(time.time() * 1000)) + exp_seconds * 1000,\n",
    "        \"timestamp\": int(round(time.time() * 1000)),\n",
    "    }\n",
    " \n",
    "    return jwt.encode(\n",
    "        payload,\n",
    "        secret,\n",
    "        algorithm=\"HS256\",\n",
    "        headers={\"alg\": \"HS256\", \"sign_type\": \"SIGN\"},\n",
    "    )\n",
    "\n",
    "key = generate_token('写入你的key', 100) # 100 为过期时间 单位为秒"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "4aa44cd9-14b5-4999-b100-5782abf4311d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Status Code 200\n",
      "JSON Response  {'choices': [{'finish_reason': 'stop', 'index': 0, 'message': {'content': '《魔法森林的奇妙之旅》\\n\\n从前，有个小女孩叫莉莉，她有一颗善良的心，热爱帮助他人。一天，她在森林里迷路了，来到了一个神秘的魔法森林。\\n\\n这片森林里的树木会说话，小动物们会跳舞，一切都充满了神奇。莉莉感到十分惊奇，她忍不住去探索这个未知的世界。\\n\\n在魔法森林里，莉莉遇到了一只受伤的小兔子。她心疼地帮小兔子包扎伤口，并给它喂水喝。小兔子感激地说：“谢谢你，莉莉。你有一颗善良的心，我将送你一颗魔法种子，它会帮助你找到回家的路。”\\n\\n莉莉接过魔法种子，小心翼翼地把它种在了土里。不久，种子发芽了，长出了一棵奇妙的小树。树上结满了各种颜色的果实，每个果实都散发着诱人的香气。\\n\\n莉莉好奇地品尝了一个红色的果实，她发现果实里竟然包含着丰富的知识。她越吃越聪明，学会了很多有趣的事情。绿色的果实让她拥有了强大的想象力，蓝色的果实让她变得更加勇敢，黄色的果实则让她懂得了关爱他人。\\n\\n莉莉在魔法森林里度过了快乐的日子，她不断地学习，成长。终于，她找到了回家的路。在离开魔法森林之前，小兔子告诉她：“莉莉，你已经拥有了善良、智慧、勇敢和关爱，这些品质将伴随你一生。请记住，永远保持一颗善良的心，去帮助更多的人。”\\n\\n莉莉回到了现实世界，她把在魔法森林学到的知识和道理分享给了身边的朋友。她用善良的心感染着身边的人，让这个世界变得更加美好。\\n\\n从此，莉莉成为了一名优秀的学生，她努力学习，成绩优异。她还经常参加志愿者活动，帮助那些需要帮助的人。她坚信，只要永远保持一颗善良的心，就能让生活充满阳光。\\n\\n这个故事告诉我们，善良、智慧、勇敢和关爱是我们人生中最宝贵的财富。只要我们保持一颗善良的心，努力学习，就能成为一个对社会有贡献的人。让我们一起，激发学习兴趣，张开想象的翅膀，去探索这个美好的世界吧！', 'role': 'assistant'}}], 'created': 1705487415, 'id': '8305988669132247242', 'model': 'glm-4', 'request_id': '8305988669132247242', 'usage': {'completion_tokens': 416, 'prompt_tokens': 61, 'total_tokens': 477}}\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "\n",
    "url = \"https://open.bigmodel.cn/api/paas/v4/chat/completions\"\n",
    "\n",
    "headers = {\n",
    "  'Content-Type': 'application/json',\n",
    "  'Authorization': key\n",
    "}\n",
    "\n",
    "data = {\n",
    "  \"model\": \"glm-4\",\n",
    "  \"messages\": [ \n",
    "      {\"role\": \"system\", \"content\": \"你是一个聪明且富有创造力的小说作家\"},\n",
    "      {\"role\": \"user\", \"content\": \"请你作为童话故事大王，写一篇短篇童话故事，故事的主题是要永远保持一颗善良的心，要能够激发儿童的学习兴趣和想象力，同时也能够帮助儿童更好地理解和接受故事中所蕴含的道理和价值观。\"}\n",
    "  ]\n",
    "}\n",
    "\n",
    "response = requests.post(url, headers=headers, json=data)\n",
    "\n",
    "print(\"Status Code\", response.status_code)\n",
    "print(\"JSON Response \", response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "7227d0c4-b69a-4792-bc08-4e375d5ad3a9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Status Code 200\n",
      "JSON Response  {'choices': [{'finish_reason': 'stop', 'index': 0, 'message': {'content': '从前，有一个美丽的小村庄，那里的居民生活得非常和谐。村庄里有一个聪明、善良的小女孩，名叫露西。露西喜欢和村里的孩子们一起玩耍，她总是乐于助人，和大家相处得非常融洽。\\n\\n有一天，村庄里来了一个神秘的老人。老人的脸上布满了皱纹，眼睛却炯炯有神。他手里拿着一本厚厚的书籍，这本书里记载着许多神奇的故事和传说。老人说，他愿意为村民们讲一个故事，但是要求大家听完故事后，要从中领悟到一个道理。\\n\\n村民们围坐在一起，老人开始讲述一个关于善良的心的故事。故事里，一个王子因为贪婪和自私，失去了善良的心，变得冷酷无情。他的国家也因此陷入了混乱和困境。而一个普通的农民，虽然生活贫穷，却因为拥有一颗善良的心，得到了神仙的帮助，最终过上了幸福的生活。\\n\\n听完故事，村民们纷纷感慨万分。露西更是深受启发，她决定要永远保持一颗善良的心，帮助身边需要帮助的人。\\n\\n从那以后，露西更加努力学习，她渴望掌握更多的知识和技能，以便更好地为村庄做贡献。她学会了医病救人，帮助农民耕种，还教村里的孩子们学习。在她的帮助下，村庄变得更加美好，人们的生活也变得更加幸福。\\n\\n有一天，露西在森林里散步，她发现了一只受伤的小鸟。她小心翼翼地把小鸟带回家里，照顾它直到康复。小鸟感激不已，它告诉露西，自己的主人是一位神仙，他愿意满足露西一个愿望。\\n\\n露西想了想，她说：“我希望村庄里的孩子们都能热爱学习，追求知识，成为有用的人。”神仙被露西的善良和智慧所感动，他答应了露西的愿望。\\n\\n从此，村庄里的孩子们都爱上了学习，他们纷纷向露西请教，追求知识的道路。村庄的教育水平不断提高，人们的生活也越来越好。\\n\\n露西始终保持着善良的心，她用自己的行动感染着身边的人。在她的带领下，村庄成为了一个充满爱心、和谐美好的地方。而露西，也成为了大家心中的童话故事大王。\\n\\n这个故事告诉我们，永远保持一颗善良的心，学会关爱他人，这样才能收获幸福和美好。同时，也要珍惜学习的机会，追求知识，成为对社会有用的人。让我们一起传递善良，创造美好的世界吧！', 'role': 'assistant'}}], 'created': 1705487011, 'id': '8308571181427835691', 'model': 'glm-3-turbo', 'request_id': '8308571181427835691', 'usage': {'completion_tokens': 490, 'prompt_tokens': 60, 'total_tokens': 550}}\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "\n",
    "url = \"https://open.bigmodel.cn/api/paas/v4/chat/completions\"\n",
    "\n",
    "headers = {\n",
    "  'Content-Type': 'application/json',\n",
    "  'Authorization': key\n",
    "}\n",
    "\n",
    "data = {\n",
    "  \"model\": \"glm-3-turbo\",\n",
    "  \"messages\": [ \n",
    "      {\"role\": \"system\", \"content\": \"你是一个聪明且富有创造力的小说作家\"},\n",
    "      {\"role\": \"user\", \"content\": \"请你作为童话故事大王，写一篇短篇童话故事，故事的主题是要永远保持一颗善良的心，要能够激发儿童的学习兴趣和想象力，同时也能够帮助儿童更好地理解和接受故事中所蕴含的道理和价值观。\"}\n",
    "  ]\n",
    "}\n",
    "\n",
    "response = requests.post(url, headers=headers, json=data)\n",
    "\n",
    "print(\"Status Code\", response.status_code)\n",
    "print(\"JSON Response \", response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "59b2370c-5901-4ee5-aeda-19460a24d57d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Status Code 200\n",
      "JSON Response  {'data': [{'embedding': [-0.013840336352586746, 0.050973281264305115, -0.01606978476047516, -0.023173445835709572, 0.010980673134326935, -0.008409803733229637, -0.04177127033472061, -0.017274904996156693, 0.005274270661175251, 0.06778223812580109, 0.02828308753669262, 0.007300905883312225, 0.012086892500519753, 0.05264405906200409, -0.04144914820790291, -0.0107622304931283, 0.011162856593728065, 0.01041372213512659, 0.04429049789905548, -0.04369046911597252, 0.025944514200091362, 0.03629264608025551, 0.003921781666576862, -0.022125720977783203, -0.0015604185173287988, -0.05565837025642395, -0.03100697509944439, -0.01982254348695278, 0.01422557607293129, -0.03273096680641174, -0.008275809697806835, -0.016490569338202477, 0.0030155268032103777, 0.01925518549978733, -0.0043815188109874725, -0.00492432713508606, -0.01396256498992443, 0.027078552171587944, -0.04922840744256973, -0.018317289650440216, 0.030475277453660965, -0.034271568059921265, 0.015852879732847214, -0.03945104777812958, 0.06451737135648727, -0.0017968181055039167, 0.056638434529304504, 0.05666719377040863, -0.011509551666676998, -0.008750321343541145, 0.03296658396720886, 0.027967577800154686, 0.0037733896169811487, 0.019928157329559326, -0.003031698754057288, -0.019426072016358376, -0.019286412745714188, 0.012573093175888062, -0.0006691195303574204, 0.02834707871079445, 0.013515694066882133, 0.017359398305416107, 0.011345638893544674, 0.0004917352343909442, 0.009153279475867748, -0.002614879049360752, -0.038334283977746964, -0.009173402562737465, -0.03669511526823044, 0.004622852895408869, 0.010900373570621014, 0.03681934252381325, -0.03974171727895737, 0.005959770642220974, -0.0350935198366642, -0.07576765865087509, -0.02436911314725876, 0.01775830239057541, 0.032508183270692825, 0.049941785633563995, 0.07232833653688431, 0.05087366700172424, -0.03165116533637047, -0.047031547874212265, -0.004178035072982311, -0.020015744492411613, -0.0028904348146170378, 0.035773299634456635, -0.035150133073329926, 0.010799368843436241, -0.017959672957658768, 0.054391078650951385, -0.01203025970607996, -0.06645682454109192, -0.03954761475324631, 0.0005603315075859427, 0.016406791284680367, -0.002548344898968935, 0.01157725416123867, -0.022010961547493935, -0.011014314368367195, -0.03288556635379791, 0.0010598250664770603, 0.023106234148144722, -0.001317499321885407, -0.025212788954377174, 0.005283980630338192, -0.027394656091928482, 0.012954461388289928, -0.07736710458993912, 0.021395668387413025, -0.006078910548239946, -0.01538024004548788, 0.023278074339032173, 0.0263171698898077, 0.011902060359716415, -0.006850844249129295, 0.05092607066035271, -0.04142746701836586, -0.01319049671292305, 0.019304949790239334, -0.021656392142176628, -0.004471915308386087, 0.025327110663056374, -0.02398936077952385, -0.005621396005153656, 0.008212468586862087, 0.007525364402681589, 0.005635066889226437, -0.060794148594141006, 0.03550507500767708, -0.014044753275811672, -0.05013267695903778, 0.026442378759384155, -0.04388171434402466, 0.03026328608393669, 0.009057535789906979, 0.006607504095882177, -0.02085733413696289, 0.008089176379144192, 0.01571827195584774, 0.024910392239689827, 0.008874461054801941, -0.0021453252993524075, 0.07129080593585968, 0.0023854209575802088, -0.03341536223888397, 0.03727695718407631, 0.014681174419820309, 0.027164548635482788, -0.017424998804926872, 0.03131147846579552, 9.50775807910531e-13, -0.03129991516470909, 0.004393821582198143, 0.0038819415494799614, -0.056948162615299225, -0.018929336220026016, 0.034847192466259, -0.004163442645221949, -0.01011746097356081, 0.008939802646636963, 0.024632981047034264, 7.641902298806858e-18, -0.0024750230368226767, -0.0023079044185578823, -0.014109078794717789, -0.03665986657142639, 0.06615221500396729, -0.023985158652067184, 0.04705943167209625, 0.009551987051963806, 0.013857991434633732, -0.039679158478975296, 0.0014700538013130426, -0.03422277793288231, 0.041574474424123764, -0.0159025676548481, -0.03136596828699112, 0.01100024115294218, 0.024235539138317108, 0.024020839482545853, 0.009893584996461868, -0.02540316991508007, -0.027352936565876007, 0.01236670557409525, -0.013493530452251434, -0.04717407375574112, -0.053392574191093445, 0.019493602216243744, -0.021438080817461014, -0.018865317106246948, -0.003030171850696206, 0.025424789637327194, 0.01875278167426586, -0.014397080056369305, -0.01764572225511074, 0.001157417893409729, -0.013583612628281116, 0.05378003418445587, -0.028847381472587585, -0.01466347835958004, 0.002599459607154131, 0.04117426648736, 0.03998955711722374, -0.005235771648585796, 0.03212064132094383, -0.027237193658947945, 0.02679678611457348, -0.009365812875330448, 0.006536250934004784, 0.028680400922894478, -0.007769394665956497, -0.06149796396493912, -0.04172876104712486, 0.049952972680330276, 0.044170863926410675, 0.02060081996023655, 0.00930344220250845, 0.003789515933021903, -0.022900206968188286, -0.0005486338632181287, -0.009221233427524567, 0.007610128726810217, -0.06515214592218399, -0.033569417893886566, -0.043491728603839874, 0.026511751115322113, 0.05828017368912697, 0.053964655846357346, -0.019239729270339012, 0.05099344626069069, 0.007248342037200928, 0.05619604140520096, -0.03701747953891754, -0.01641182415187359, 0.018887866288423538, -0.0010599076049402356, -0.00350874662399292, 0.005433203186839819, -0.007090877741575241, 0.030686482787132263, -0.04464227706193924, 0.03166237473487854, 0.05134950950741768, -0.022835612297058105, 0.016232822090387344, -0.016425779089331627, -0.019658222794532776, -0.02039528638124466, -0.02794390730559826, -0.03650254011154175, -0.0612548291683197, 0.01835928112268448, 0.013324051164090633, 0.06372737884521484, 0.007689506746828556, 0.05190238729119301, 0.010629691183567047, -0.027475159615278244, -0.017768435180187225, 0.026483947411179543, 0.025500604882836342, -0.010916256345808506, 0.04931006208062172, -0.03386848792433739, 0.022304413840174675, 0.025764016434550285, -0.013255405239760876, -0.04517526924610138, 0.011028984561562538, 0.007426601834595203, 0.01286130491644144, 0.025128822773694992, 0.031164707615971565, 0.030500546097755432, -0.010354584082961082, -0.033448491245508194, 0.021189140155911446, 0.02135321870446205, -0.018690425902605057, -0.024369163438677788, 0.027370359748601913, 0.03542836010456085, -0.03194206580519676, 0.0003650795842986554, -0.02463042177259922, 0.025195838883519173, -0.01569369249045849, 0.017522260546684265, -0.032798994332551956, -0.0053775729611516, -0.01712428592145443, 0.06196778267621994, -0.027424084022641182, 0.04694744199514389, -0.04604674130678177, -0.022697689011693, 0.009681147523224354, -0.01251052413135767, 0.044813528656959534, -0.0065089138224720955, 0.012795745395123959, 0.03371601924300194, -0.02099360153079033, -0.030577553436160088, 0.0020013770554214716, 0.03779851645231247, 0.013849717564880848, 0.019237767904996872, 0.0019106912659481168, 0.02209651842713356, -0.004635978490114212, 0.0016024088254198432, -0.03964284062385559, -0.008211316540837288, -0.06292866170406342, -0.01615050621330738, 0.03395003080368042, -0.02021116204559803, -0.05231892317533493, 0.023687440901994705, -0.0007332255481742322, -0.043275706470012665, 0.008234359323978424, -0.031195543706417084, -0.017902188003063202, 0.0218934565782547, 0.014377052895724773, -0.0036111201625317335, -0.004414299037307501, -0.051577817648649216, 0.03917592763900757, -0.019542628899216652, -0.032631006091833115, 0.018827222287654877, -0.03971460834145546, -0.029347684234380722, 0.019161267206072807, -0.015826715156435966, 0.046570099890232086, 0.020839117467403412, -0.026985518634319305, 0.046629153192043304, -0.0027720294892787933, 0.03133417293429375, -0.02123831771314144, -0.04467444121837616, 0.02435792051255703, 0.04276047274470329, 0.06714294105768204, -0.03952573612332344, 0.007964059710502625, 0.02425728365778923, 0.01791614666581154, 0.0660494938492775, 0.057423561811447144, 0.003972407430410385, 0.01434845570474863, 0.022631589323282242, 0.023245546966791153, -0.027296120300889015, 0.03140826150774956, 0.008361437357962132, -0.042488038539886475, 0.010568201541900635, -0.037346940487623215, 0.03336302563548088, -0.046387504786252975, -0.03187880665063858, 0.04131225496530533, 0.012293259613215923, 0.01118265651166439, 0.07671091705560684, -0.058087557554244995, -0.022715497761964798, 0.020794764161109924, -0.07673251628875732, -0.002262922702357173, -0.003064698539674282, 0.00874873623251915, -0.03785761445760727, -0.04160512983798981, -0.004704820923507214, 0.025635529309511185, 0.0012369471369311213, 0.025386853143572807, -0.02188953571021557, -0.006494353525340557, 0.05355570837855339, -0.044686008244752884, -0.012007315643131733, 0.016703084111213684, -0.011354213580489159, -0.060814108699560165, 0.030856475234031677, -0.020137157291173935, 0.019490277394652367, 0.018335705623030663, 0.020337872207164764, -0.008714109659194946, -0.02025516703724861, -0.041874829679727554, -0.02930876426398754, 0.011664455756545067, -0.02877493016421795, 0.016664810478687286, -0.027864426374435425, -0.04292679950594902, 0.01764233596622944, -0.006294396705925465, -0.05375804379582405, -0.002676247153431177, -0.029690047726035118, -0.016505694016814232, 0.01728653721511364, 0.018356382846832275, -0.014840400777757168, 0.0031903621274977922, 0.010630026459693909, -0.020340893417596817, -0.022514376789331436, 0.009038779884576797, -0.03743518888950348, 0.05843159928917885, 0.0025972051080316305, -0.0008243306656368077, 0.012918987311422825, -0.011834331788122654, -0.07620573043823242, -0.023417562246322632, 0.034534525126218796, -0.04580890014767647, -0.0011077991221100092, -0.00405442900955677, -0.029311254620552063, -0.01487817708402872, -0.04389100894331932, 0.049376823008060455, 0.027562232688069344, -0.02382843568921089, 0.02126748487353325, 0.029167504981160164, -0.018559284508228302, 0.0021007731556892395, -0.018943432718515396, -0.003215564414858818, -0.05625276267528534, 0.026062753051519394, -0.015747014433145523, 0.03578661009669304, 0.03504742681980133, 0.04957791790366173, 0.011277178302407265, 0.009004984982311726, -0.023038411512970924, -0.011601557955145836, -0.02393832802772522, 0.034126803278923035, -0.04522740840911865, -0.00535098509863019, -0.022627245634794235, 0.04372492432594299, 0.0002523419971112162, -0.016567859798669815, -0.06087474524974823, -0.002081796759739518, 0.024798467755317688, -0.005074472166597843, 0.002257891930639744, 0.02301129512488842, -0.033024903386831284, 0.057207994163036346, -0.029571248218417168, 0.0136718163266778, -0.0063619003631174564, -0.03609084337949753, 0.03804018348455429, 0.006645363289862871, 0.007369581609964371, 0.005669642239809036, -0.008494116365909576, -0.020445706322789192, 0.03534455969929695, -0.053996581584215164, -0.011183864437043667, -0.058757465332746506, -0.00421109376475215, 0.037061236798763275, -0.005172538571059704, 0.04636358097195625, -0.008042124100029469, 0.006049544550478458, 0.012627039104700089, 0.02772071212530136, -0.04464055970311165, -0.009328773245215416, -0.007336387410759926, 0.04739244282245636, 0.029776031151413918, 0.018928900361061096, -0.06558848917484283, -0.001851567649282515, 0.019900623708963394, -0.019058896228671074, -0.002876093378290534, 0.06924401223659515, -0.006381096784025431, -0.019729863852262497, -0.03426118940114975, 0.022420674562454224, -0.024105092510581017, 0.06867748498916626, 0.006945006549358368, 0.020196417346596718, 0.021045641973614693, 0.00028268026653677225, 0.03665751591324806, -0.0468863844871521, -0.023013722151517868, 0.004747933242470026, -0.0014406053815037012, 0.0070180571638047695, 0.012754850089550018, -0.01997450366616249, -0.03692101687192917, 0.0426187589764595, 0.04481435567140579, -0.030336108058691025, -0.014216044917702675, -0.024792350828647614, -0.045836370438337326, 0.04464124143123627, -0.021294735372066498, -0.010435168631374836, 0.00015046064800117165, -0.03027649223804474, 0.018287867307662964, -0.02204550802707672, -0.00742313452064991, 0.061672136187553406, 0.020027119666337967, -0.018956879153847694, 0.01122264377772808, -0.014037990011274815, 0.02629404515028, -0.007882669568061829, -0.013922031037509441, -0.0507272444665432, 0.04266279935836792, -0.015729933977127075, -0.01460945699363947, -0.05892365053296089, 0.022241879254579544, -0.031862422823905945, -0.017258068546652794, 0.0007502220687456429, 0.010146579705178738, -0.019248927012085915, 0.05625446140766144, -0.054284580051898956, -0.045057281851768494, 0.043454766273498535, -0.015399504452943802, -0.031525932252407074, -0.03240527957677841, 0.009238327853381634, 0.030254118144512177, 0.03633944317698479, 0.0027230652049183846, 0.007631582673639059, -0.00042152334935963154, 0.046122919768095016, 0.018859336152672768, -0.015769796445965767, 0.03794235363602638, -0.020785721018910408, -0.020413385704159737, -0.042471855878829956, 0.003988320007920265, 0.012182333506643772, 0.05154647305607796, -0.09591282159090042, -0.013946284539997578, -0.01812005043029785, -0.030557135120034218, 0.03782777115702629, 0.03957619518041611, 0.02531241439282894, 0.0319896936416626, -0.01763894408941269, -0.021165726706385612, 0.040948059409856796, 0.05615448206663132, 0.006887488532811403, 0.05616893246769905, 0.007286179810762405, 0.004830565303564072, 6.348972237901762e-05, 0.0609266497194767, 0.0005299162003211677, -0.014149940572679043, -0.04715478792786598, -0.009106133133172989, 0.013854718767106533, 0.021631717681884766, -0.07151927798986435, -0.0036912395153194666, 0.0007219529361464083, -0.01604827120900154, -0.007915385998785496, -0.009195327758789062, -0.0450466051697731, -0.008408634923398495, -0.008480338379740715, -0.018165336921811104, 0.020840873941779137, 0.01842784695327282, -0.001818906981498003, 0.01659340038895607, -0.06349414587020874, 0.0011159327114000916, 0.06470037251710892, -0.02210419438779354, -0.043513309210538864, -0.011716548353433609, 0.04438050463795662, 0.05714958533644676, -0.04744179919362068, -0.0025732750073075294, 0.003436719998717308, 0.005430266261100769, -0.007882901467382908, 0.005987163167446852, -0.021746767684817314, -0.033076491206884384, 0.03616135194897652, -0.02317538857460022, 0.027410753071308136, 0.010679165832698345, -0.0022690934129059315, -0.01675650104880333, 0.025106700137257576, -0.029681457206606865, 0.021695556119084358, 0.01470709778368473, -0.03457013517618179, -0.04475210979580879, -0.03019486740231514, 0.011157091706991196, 0.07525080442428589, 0.0010057426989078522, -0.017226362600922585, 0.03103717416524887, -0.02135671302676201, 0.014235248789191246, -0.027762824669480324, 0.024700185284018517, -0.00988817773759365, -0.007272745482623577, 0.020390840247273445, -0.005575001705437899, -0.019264016300439835, 0.03434831276535988, -0.074650838971138, -0.06447886675596237, -0.011431466788053513, -0.04939615726470947, 0.011172492057085037, -0.07712392508983612, 0.0005030845641158521, 0.014755835756659508, -0.02340378612279892, -0.025708448141813278, -0.012760247103869915, -0.047022998332977295, -0.014411022886633873, -0.025506360456347466, 0.022578371688723564, 0.007992220111191273, 0.017150839790701866, 0.012615203857421875, -0.046810537576675415, -0.00962601788341999, 0.0003165242378599942, -0.0077263242565095425, -0.04585270583629608, 0.04321363940834999, 0.00452771270647645, -0.04928581789135933, 0.014986129477620125, -0.0007964531541801989, -0.0029018973000347614, -0.02842293307185173, 0.03143071383237839, -0.0009777238592505455, 0.012978878803551197, 0.05991677939891815, 0.013999742455780506, -0.012250643223524094, 0.014959047548472881, -0.012565654702484608, 0.0045063612051308155, -0.014648023061454296, -0.006123183760792017, -0.04699854180216789, -0.034826479852199554, 0.047904904931783676, 0.038414206355810165, -0.0002526442112866789, 0.033410295844078064, -0.005168129689991474, 0.01621105708181858, 0.0611257329583168, 0.0036854734644293785, -0.017901211977005005, -0.024189436808228493, -0.012483768165111542, -0.0690189078450203, 0.0028107608668506145, -0.018379690125584602, 0.020128656178712845, -0.004103942774236202, 0.03924540430307388, -0.010608245618641376, 0.021965621039271355, -0.007825764827430248, 0.04064586013555527, 0.00829179398715496, -0.025932999327778816, 0.043395210057497025, -0.06926847994327545, 0.007469095755368471, -0.022667568176984787, 0.025965282693505287, 0.0013914245646446943, -0.005779601167887449, -0.007419684901833534, 0.002594273304566741, -0.04141971096396446, -0.05085347220301628, -0.018638212233781815, 0.06441307812929153, -0.011697477661073208, -0.01580243930220604, -0.007602511905133724, 0.0262187160551548, -0.0020414043683558702, 0.0030491692014038563, 0.029039008542895317, -0.022611724212765694, 0.003258936107158661, -0.011477284133434296, 0.014792779460549355, 0.014387176372110844, 0.0038511445745825768, 0.020729539915919304, 0.010817092843353748, 0.020376596599817276, 0.05826157331466675, -0.014190522953867912, -0.06213734298944473, -0.002572153229266405, 0.016537513583898544, -0.01953989826142788, -0.018903162330389023, -0.03562745079398155, 0.005927611142396927, -0.040105532854795456, -0.07001616805791855, -0.04728883504867554, 0.03754444792866707, -0.018085753545165062, 0.032127659767866135, 0.019636135548353195, -0.010362938046455383, -0.022123059257864952, 0.0050790696404874325, 0.013359593227505684, -0.07238603383302689, 0.019633177667856216, -0.03617604821920395, -0.034877974539995193, 0.00913973804563284, -0.05924217402935028, -0.019159220159053802, -0.0003683375252876431, 0.028997965157032013, 0.028473377227783203, 0.009556842036545277, 0.006702473387122154, -0.05037027224898338, -0.057548463344573975, -0.013351226225495338, -0.01788988709449768, 0.01978614367544651, -0.04704081267118454, -0.08745197206735611, 0.08612208813428879, 0.0382087342441082, 2.3911501667583934e-09, -0.0665915384888649, 0.014344657771289349, -0.007130180019885302, 0.03487984836101532, 0.019330905750393867, -0.07471887022256851, 0.024007480591535568, 0.006538156419992447, 0.018326427787542343, -0.034261804074048996, 0.0004517052148003131, -0.010348173789680004, 0.056575044989585876, -0.01777832768857479, 0.033933695405721664, 0.044201094657182693, 0.030334917828440666, 0.032965242862701416, 0.04823102056980133, -0.007149853277951479, 0.039890144020318985, 0.022329701110720634, -0.01846790872514248, 0.08204057812690735, 0.04021430015563965, 0.03324151411652565, 0.018567344173789024, -0.0025344539899379015, 0.02313707210123539, -0.004470893647521734, 0.05229289457201958, -0.01547122560441494, -0.007926514372229576, -0.03282185643911362, -0.014267349615693092, -0.04760430380702019, -0.0053824009373784065, 0.005132417660206556, -0.01688743568956852, 0.05107705295085907, 0.026235481724143028, -0.03180844709277153, 0.10623346269130707, -0.025637485086917877, 0.008492554537951946, -0.004122650716453791, -0.0029551261104643345, 0.012488550506532192, 0.005234993528574705, 0.03746345639228821, -0.053235236555337906, -0.002989779692143202, -0.022389966994524002, 0.033577051013708115, 0.01713613234460354, -0.038250572979450226, 0.03154674917459488, -0.06723015010356903, 0.007145960349589586, -0.03276506066322327, -0.0702965036034584, 0.07251841574907303, -0.009776726365089417, -0.0011555575765669346, -0.035601906478405, 0.0826004222035408, 0.06345485895872116, -0.029085515066981316, 0.015618523582816124, -0.0071302964352071285, -0.023413628339767456, 0.0014767643297091126, 0.015516757033765316, 0.006390698719769716, 0.0400167740881443, -0.03323643282055855, -0.042473793029785156, -0.026247715577483177, 0.004487179685384035, 0.02245398610830307, 0.03067692555487156, -0.01704472117125988, 0.008191164582967758, -0.040783051401376724, -0.0011500774417072535, -0.04148305207490921, 0.0065286471508443356, -0.07289881259202957, 0.013037214055657387, 0.01816076599061489, 0.014638754539191723, 0.0337672159075737, -0.03080420382320881, 0.04005139321088791, -0.07639206200838089, -0.03395123407244682, 0.03734985366463661, 0.008692256174981594, 0.005942227318882942, -0.009689651429653168, 0.005448247771710157, 0.0031116309110075235, -2.0620718136330307e-27, 0.028212105855345726, -0.06236269697546959, 0.04996993765234947, 0.016931941732764244, 0.03738127276301384, -0.006869104225188494, 0.015902556478977203, 0.029635680839419365, 0.010746786370873451, 0.0069756279699504375, 0.0466499961912632, -0.0673741027712822, -0.009450288489460945, 0.017871493473649025, 0.02308395877480507, -0.01825137808918953, 0.033791325986385345, 0.013271667063236237, 0.016756853088736534, -0.022977830842137337, -0.08855298161506653, -0.011348037980496883, 0.009987415745854378, 0.04277486354112625, -0.00847655814141035, 0.012157999910414219, 0.006239320617169142, 0.009486806578934193, -0.02471543289721012, -0.07689458876848221, 0.0026139768306165934, -0.015456456691026688, 0.0060494765639305115, 0.035513266921043396, -0.005110384896397591, 0.0018820735858753324, -0.0012274989858269691, 0.005637597758322954, -0.027610912919044495, 0.037385243922472, 0.004805353935807943, 0.012100188061594963, -0.03493720293045044, -0.019846677780151367, -0.007176665123552084, -0.04522005096077919, 0.01156238466501236, 0.04348788410425186, 0.022847235202789307, -0.03370991349220276, 0.038688208907842636, 0.018021784722805023, -0.049247439950704575, -0.0017489931778982282, 0.030196208506822586, 0.018747108057141304, -0.027852382510900497, -0.012423540465533733, -0.013113269582390785, 0.011794804595410824, 0.0054970234632492065, 0.014661487191915512, 0.011326351203024387, 0.015095227397978306, 0.01986338384449482, 0.013337196782231331, 0.04365181550383568, -0.030632279813289642, -0.03376287221908569, 0.019675662741065025, 0.05043915659189224, 0.010421947576105595, 0.03482026234269142, 0.01734684221446514, 0.037498850375413895, -0.018590759485960007, 0.018741559237241745, -0.01946088671684265, 0.00953187607228756, 0.05444883555173874, 0.0074784778989851475, 0.022880036383867264, 0.00768275698646903, -0.023291409015655518, -0.004124866332858801, -0.030415844172239304, 0.026256799697875977, 0.05622875317931175, -0.008891341276466846, -0.008672446012496948, 0.043793365359306335, 0.026584787294268608, -0.017408305779099464, 0.05345722287893295, -0.04076004400849342, -0.05226759985089302, -0.009754737839102745, 0.028841456398367882, -0.020593993365764618, 0.025332430377602577, -2.656698343686867e-24, 0.0047985645942389965, 0.029686393216252327, -0.01301286369562149, 0.011895045638084412, 0.024041535332798958, -0.004486155230551958, -0.020390374585986137, -0.046249158680438995, -0.02151542156934738, -0.08704075962305069, 0.06536786258220673, -0.03121890313923359, 0.04064856469631195, 0.06892218440771103, -0.02387186326086521, -0.009251397103071213, 0.02990889362990856, -0.007868190295994282, 0.02838939242064953], 'index': 0, 'object': 'embedding'}], 'model': 'embedding-2', 'object': 'list', 'usage': {'completion_tokens': 0, 'prompt_tokens': 9, 'total_tokens': 9}}\n",
      "CPU times: user 25.1 ms, sys: 6.21 ms, total: 31.3 ms\n",
      "Wall time: 907 ms\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "\n",
    "import requests\n",
    "url = \"https://open.bigmodel.cn/api/paas/v4/embeddings\"\n",
    "\n",
    "headers = {\n",
    "  'Content-Type': 'application/json',\n",
    "  'Authorization': key\n",
    "}\n",
    "\n",
    "data = {\n",
    "  \"model\": \"embedding-2\",\n",
    "  \"input\": \"测试文本，今天很开心。\"\n",
    "}\n",
    "\n",
    "response = requests.post(url, headers=headers, json=data)\n",
    "\n",
    "print(\"Status Code\", response.status_code)\n",
    "print(\"JSON Response \", response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "08a165a2-811f-4d34-a01d-db249f2965bc",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
